// pages/addContract/addContract.js

//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    client:[],
	contacts:[],
	contract:[],
	provinces:[],
	users:[],
	
	sfqq:['否','是'],
	index:0,
	
	date1:'', //签订日期
	date2:'', //合同到期时间
	
	qysfId:'', //签约省份id
	qysqList:[], //签约市区下拉
	qysqId:'', //签约市区id
	index1:0,
	index2:0,
	search:'',
	cyrinput:'',
	
	sfCode:'',
	sqCode:'',
	
	fileUrl:null,
	fileName:null,
	filepostName:null,
	
	hidden:true,

    loginName: app.globalData.loginName
  },
  bindPickerChange: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,      
    })
  },
  bindPickerChange1: function (e) {
	var that = this;
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
	
	if( e.detail.value==0){
		that.setData({
			index1: e.detail.value,
			index2:0,
			qysqId:null,
			qysqList:null,
		})
		return
	}
    
	wx.request({
		url:app.globalData.apiUrl+'/wx/wxsignRegion',
		data:{
			loginName:app.globalData.loginName,
			id:that.data.provinces[e.detail.value].id,
		},
		success:function(res){
			console.log(res);
			var mydata=res.data.data;
			if(mydata.length == 0){
				that.setData({
					index1: e.detail.value,
					index2:0,
					qysqId:null,
					qysqList:null,
				})
				return
			}
			that.setData({
				qysqId:mydata[0].id,
				qysqList:mydata,
				index2:0,
			})
		}
	})	
	
	this.setData({
	  index1: e.detail.value,
	  qysfId: this.data.provinces[e.detail.value].id,
	  // qysqId: this.data.qysqList[0].id
	})
  },
  bindPickerChange2: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index2: e.detail.value,
      qysqId: this.data.qysqList[e.detail.value].id
    })
  },
  bindDateChange1: function (e) {
    this.setData({
      date1: e.detail.value
    })
  },
  bindDateChange2: function (e) {
    this.setData({
      date2: e.detail.value
    })
  },

  // 上传
  bindFilewenjian: function (e) {   
    var that = this; 
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        var fileBDsrc = res.tempFiles[0].path;
        var fileBDname = res.tempFiles[0].name;        
        wx.uploadFile({
          url: app.globalData.apiUrl+'/wx/wxscgn?filename='+fileBDname, //仅为示例，非真实的接口地址
          filePath: fileBDsrc,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success(res) {
            const data = res.data;       
            var jsondata = JSON.parse(data);     
            //do something
            wx.showToast({
              title: '上传成功',
              icon: 'success',
              duration: 2000
            })
            that.setData({
              fileName:fileBDname,
              fileUrl:jsondata.msg,
            })
          },
          fail(e){
            wx.showToast({
              title: '网络繁忙请重新上传!',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  },
  // 客户名称模糊搜索
  changeSearch: function (event) {
    console.log(event.detail);
    let that = this;
    var inputSearch = event.detail.value;
    that.setData({
      search: inputSearch
    })
    that.getKhmc(inputSearch);
  },
  // 点击客户名称
  aKhmc: function (e) {
    console.log(e);
    this.setData({
      cyrinput: e.target.dataset.text,
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
	this.getContractDetial(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit: function (e) {
    var that = this; 
   
  
    wx.showToast({
      icon: 'loading',
      title: 'Loading',
      duration: 2000,
      mask: true
    })
  
    console.log(this.data.clientName);
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    let { loginName, Id, isDefect, customerContact,customerContactPhone,signDate,deadline,expirationDate,signer,signProvince,signRegion,customerOfficerOne,customerOfficerTwo,participant,url,remark} = e.detail.value;
    if (!customerContact || !customerContactPhone || !signDate || !deadline || !expirationDate || !signer || !signProvince || !signRegion || !customerOfficerOne || !customerOfficerTwo || !participant ) {
      wx.showToast({
        title: '请填写完整信息！',
        icon: 'none',
        duration: 2000
      })
      //wx.hideToast();
      return;
    }
    wx.request({
      url: app.globalData.apiUrl + '/wx/wxeditDo', //接口地址
      data: e.detail.value,
      success: function (res) {
        console.log("新增客户信息=", res)
       if(res.data.code == 0){         
         wx.showToast({
           title: '操作成功！',
           icon: 'success',
           duration: 2000		   
         })
  		
       }else{
         wx.showToast({
           title: res.data.msg,
           icon: 'none',
           duration: 2000
         })
       }
        setTimeout(function(){
        	var pages = getCurrentPages();//获取页面栈
        	if (pages.length > 1) {
        	  //上一个页面实例对象
        	  var prePage = pages[pages.length - 2];
        	  //调用上一个页面的onShow方法
        	  prePage.onLoad();
        	}
        	wx.navigateBack({
        	  complete: (res) => {},
        	}) 
        },2000)	
      }
    })
  },
  
  getContractDetial:function(codes){
	var that = this;
	var sfcode=null;
	var sqcode=null;
	wx.request({
	  url: app.globalData.apiUrl + '/wx/wxpactinfoedit', //接口地址
	  data:{
	    loginName: app.globalData.loginName,
	    id:codes,
	  }, 
	  success: function (res) {
	    console.log("数据字典=", res);
	    var mydata=res.data.data;
		sfcode = mydata.contract.signProvince;
		sqcode = mydata.contract.signRegion;
		var scUrl=mydata.contract.url;
		if(scUrl != null && scUrl !=""){
			that.setData({
				fileUrl:scUrl,
				fileName:mydata.urlList[0].name,
			})
		}
		mydata.provinces.unshift({'name':'请选择'});
		that.setData({
			client:mydata.client,
			contacts:mydata.contacts,
			contract:mydata.contract,
			provinces:mydata.provinces,
			users:mydata.users,
			index:mydata.contract.isDefect,
			cyrinput:mydata.contract.participant,
			sfCode:mydata.contract.signProvince,
			sqCode:mydata.contract.signRegion,
		})
		for (let i = 0; i < mydata.provinces.length; i++) {
			if(that.data.sfCode == mydata.provinces[i].id){
				that.setData({
					index1:i
				})
			}
		}
		
		wx.request({
			url:app.globalData.apiUrl+'/wx/wxsignRegion',
			data:{
				loginName:app.globalData.loginName,
				id:sfcode,
			},
			success:function(res){
				console.log(res);
				var mydata2=res.data.data;		
				if(sqcode != null){
					that.setData({
						qysqId:that.data.sqCode,
						qysqList:mydata2,
					})
					for (let y = 0; y < mydata2.length; y++) {
						if(sqcode == mydata2[y].id){
							that.setData({
								index2:y
							})
						}
					}
				}								
			}
		})
		
	  }
	})
	
	
  }

})